﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace game
{
    public class GameScreen : Screen
    {
        private Scene scene = null;
        private Hud hud = null;
        private PostProcessManager postProcessMgr = null;

        private SingleMatrixShader depthWriteShader;
        private RenderTarget2D screenSizeRenderTarget;

        public override void LoadContent(IServiceProvider serviceProvider)
        {
            base.LoadContent(serviceProvider);
            scene = SceneFactory.CreateScene(this.content);
            hud = new Hud(scene, scene.PlayerObject);
            postProcessMgr = new PostProcessManager(scene);

            depthWriteShader = new SingleMatrixShader("Effects\\DepthWrite", "DepthWrite");
            depthWriteShader.Initialize();

            PresentationParameters pp = device.PresentationParameters;
            screenSizeRenderTarget = new RenderTarget2D(device, pp.BackBufferWidth / 2, pp.BackBufferHeight / 2,
                1, pp.BackBufferFormat, RenderTargetUsage.DiscardContents);
        }

        public override void Update(GameTime gameTime)
        {

            scene.Update(gameTime);
            hud.Update(gameTime);
            //postProcessMgr.Update(gameTime);

        }

        public override void Draw(GameTime gameTime)
        {
            if (Input.Keyboard.IsKeyDown(Keys.T))
            {
                //device.Clear(Color.White);
                //scene.DrawObjectsWithCustomShader(gameTime, depthWriteShader);

                device.SetRenderTarget(0, screenSizeRenderTarget);
                device.Clear(Color.Black);
                scene.DrawObjectsWithCustomShader(gameTime, depthWriteShader);
                device.SetRenderTarget(0, null);
                device.Clear(ClearOptions.DepthBuffer, Color.White, 1.0f, 0);

                ShooterGame.Instance.SpriteBatch.Begin(SpriteBlendMode.None,
                SpriteSortMode.Immediate, SaveStateMode.None);

                ShooterGame.Instance.SpriteBatch.Draw(screenSizeRenderTarget.GetTexture(),
                    new Rectangle(0, 0,
                        device.PresentationParameters.BackBufferWidth,
                        device.PresentationParameters.BackBufferHeight),
                        Color.White);
                ShooterGame.Instance.SpriteBatch.End();
            }
            else
            {
                device.Clear(Color.Black);
                scene.Draw(gameTime);
                
                //postProcessMgr.Draw(gameTime);


                hud.Draw(gameTime);
            }

            
            

        }
    }
}
